1. /* slfprmpf.cpp by K.Tsuru */
  2. // function ID 4108 DRADIX since ver 2.182
  3. // first version made on 30 Apr 2010
  4. /**************************************************************
  5. permutation
  6. n!
  7. nPr = ---------
  8. (n - r)!
  9. is evaluated by factorizing n! and (n - r)! into prime factors
  10. **************************************************************/
  11. #ifndef SN_H
  12. #include "sn.h"
  13. #endif
  14. // for sorting
  15. static int sortByPower(const void *e1, const void *e2) {
  16. return (int)((const primeFactor *)e2)->power - (int)((const primeFactor *)e1)->power;
  17. }
  18. SLong permL(const ulong n, const ulong r) {
  19. if(r == 0) return 1.0;
  20. if(n == r) return Fact(n);
  21. if( n < r ) SNManager::SetError(SNManager::DOMAIN_ERR,"combPF()", 4108);
  22. SNBlock<primeFactor> npf, n_rpf;
  23. int in = FactorialIntoPrimeFactor(n, npf); // number of prime factor n!
  24. int in_r = FactorialIntoPrimeFactor(n - r, n_rpf);// (n-r)!
  25. // reduction
  26. for(int k = 0; k < in_r; k++) npf[k].power -= n_rpf(k).power;
  27. int non0pwr = 0; // number of non zero power elements
  28. for(int k = 0; k < in; k++) {
  29. if(npf(k).power) non0pwr++;
  30. }
  31. // pick out nonzero power elements
  32. primeFactor* wpf = new primeFactor[non0pwr+1];
  33. int el = 0;
  34. for(int k = 0; k < in; k++) {
  35. if(npf(k).power) { wpf[el] = npf(k); el++; }
  36. }
  37. wpf[el].prime = wpf[el].power = 0; // index of last element
  38. qsort(wpf, non0pwr, sizeof(primeFactor), sortByPower);
  39. SNBlock<primeFactor> pfBk(el+1);
  40. for(int k = 0; k <= el; k++) pfBk[k] = wpf[k];
  41. SLong p = PrimeFactorProduct(pfBk, el);
  42. delete[] wpf;
  43. return p;
  44. }

slfprmpf.cpp : last modifiled at 2016/10/09 10:14:39(1,600 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).